{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32mUntitled-3.ipynb 单元格 1\u001b[0m line \u001b[0;36m1\n\u001b[0;32m    <a href='vscode-notebook-cell:Untitled-3.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=100'>101</a>\u001b[0m buttonCancel\u001b[39m.\u001b[39mplace(x\u001b[39m=\u001b[39m\u001b[39m90\u001b[39m, y\u001b[39m=\u001b[39m\u001b[39m100\u001b[39m, width\u001b[39m=\u001b[39m\u001b[39m50\u001b[39m, height\u001b[39m=\u001b[39m\u001b[39m20\u001b[39m) \n\u001b[0;32m    <a href='vscode-notebook-cell:Untitled-3.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=102'>103</a>\u001b[0m \u001b[39m#启动消息循环 \u001b[39;00m\n\u001b[1;32m--> <a href='vscode-notebook-cell:Untitled-3.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=103'>104</a>\u001b[0m root\u001b[39m.\u001b[39;49mmainloop()\n",
      "File \u001b[1;32mc:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\tkinter\\__init__.py:1504\u001b[0m, in \u001b[0;36mMisc.mainloop\u001b[1;34m(self, n)\u001b[0m\n\u001b[0;32m   1502\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmainloop\u001b[39m(\u001b[39mself\u001b[39m, n\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m):\n\u001b[0;32m   1503\u001b[0m \u001b[39m    \u001b[39m\u001b[39m\"\"\"Call the mainloop of Tk.\"\"\"\u001b[39;00m\n\u001b[1;32m-> 1504\u001b[0m     \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mtk\u001b[39m.\u001b[39;49mmainloop(n)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import tkinter \n",
    "import tkinter.messagebox \n",
    "import os \n",
    "import os.path \n",
    " \n",
    "path = os.getenv('temp') \n",
    "filename = os.path.join(path, 'info.txt') \n",
    " \n",
    "#创建应用程序窗口 \n",
    "root = tkinter.Tk() \n",
    "#定义窗口大小 \n",
    "root['height'] = 140 \n",
    "root['width'] = 200 \n",
    " \n",
    "#在窗口上创建标签组件 \n",
    "labelName = tkinter.Label(root, \n",
    "                             text='User Name:', \n",
    "                             justify=tkinter.RIGHT, \n",
    "                             anchor = 'e', \n",
    "                             width=80) \n",
    "#把组件放置到窗口上指定区域 \n",
    "labelName.place(x=10, y=5, width=80, height=20) \n",
    " \n",
    "#创建字符串变量和文本框组件，同时设置关联的变量 \n",
    "#可以通过关联变量来读取或修改文本框内的文本 \n",
    "varName = tkinter.StringVar(root, value='') \n",
    "entryName = tkinter.Entry(root, \n",
    "                             width=80, \n",
    "                             textvariable=varName) \n",
    "entryName.place(x=100, y=5, width=80, height=20) \n",
    " \n",
    "labelPwd = tkinter.Label(root, \n",
    "                            text='User Pwd:', \n",
    "                            justify=tkinter.RIGHT, \n",
    "                            anchor = 'e', \n",
    "                            width=80) \n",
    "labelPwd.place(x=10, y=30, width=80, height=20) \n",
    " \n",
    "#创建密码文本框 \n",
    "varPwd = tkinter.StringVar(root, value='') \n",
    "entryPwd = tkinter.Entry(root, \n",
    "                            show='*',    #不管输入什么，都显示星号 \n",
    "                            width=80, \n",
    "                            textvariable=varPwd) \n",
    "entryPwd.place(x=100, y=30, width=80, height=20) \n",
    " \n",
    "#尝试自动填写用户名和密码 \n",
    "try: \n",
    "    with open(filename) as fp: \n",
    "        n, p = fp.read().strip().split(',') \n",
    "        varName.set(n) \n",
    "        varPwd.set(p) \n",
    "except: \n",
    "    pass \n",
    " \n",
    "#记住我，复选框 \n",
    "rememberMe = tkinter.IntVar(root, value=1) \n",
    "#选中时变量值为1，未选中时变量值为0，默认选中 \n",
    "checkRemember = tkinter.Checkbutton(root, \n",
    "                                        text='Remember me?', \n",
    "                                        variable=rememberMe, \n",
    "                                        onvalue=1, \n",
    "                                        offvalue=0) \n",
    "checkRemember.place(x=30, y=70, width=120, height=20) \n",
    " \n",
    "#登录按钮事件处理函数 \n",
    "def login(): \n",
    "    #获取用户名和密码 \n",
    "    name = entryName.get() \n",
    "    pwd = entryPwd.get() \n",
    "    if name=='admin' and pwd=='123456': \n",
    "        tkinter.messagebox.showinfo(title='恭喜', \n",
    "                                       message='登录成功！') \n",
    "        if rememberMe.get() == 1: \n",
    "            #把登录成功的信息写入临时文件         \n",
    "            with open(filename, 'w') as fp: \n",
    "                fp.write(','.join((name,pwd))) \n",
    "        else: \n",
    "            try: \n",
    "                #删除用于记录用户名和密码的临时文件 \n",
    "                os.remove(filename) \n",
    "            except: \n",
    "                pass \n",
    "    else: \n",
    "        tkinter.messagebox.showerror('警告', \n",
    "                                        message='用户名或密码错误') \n",
    "#创建按钮组件，同时设置按钮事件处理函数 \n",
    "buttonOk = tkinter.Button(root, \n",
    "                            text='Login',  #设置按钮上显示的文本 \n",
    "                            command=login) #设置按钮的单击事件处理函数 \n",
    "buttonOk.place(x=30, y=100, width=50, height=20) \n",
    " \n",
    "#取消按钮的事件处理函数 \n",
    "def cancel(): \n",
    "    #清空用户输入的用户名和密码 \n",
    "    varName.set('') \n",
    "    varPwd.set('') \n",
    "buttonCancel = tkinter.Button(root, \n",
    "                                 text='Cancel', \n",
    "                                 command=cancel) \n",
    "buttonCancel.place(x=90, y=100, width=50, height=20) \n",
    " \n",
    "#启动消息循环 \n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
